//
//  CollectViewController.m
//  FoodFamily
//
//  Created by dlios on 15-6-27.
//  Copyright (c) 2015年 guoBang. All rights reserved.
//

#import "CollectViewController.h"

@interface CollectViewController ()

@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.mytableview = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.mytableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-20-49-40) style:UITableViewStylePlain];
    self.mytableview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mytableview];
    self.mytableview.dataSource = self;
    self.mytableview.delegate = self;
    //mrc   手动释放  arc 系统自动管理内存
    [[DataBaseHandle shareDataBaseHandle]openDB];
    [[DataBaseHandle shareDataBaseHandle]createTable];//创建表
    //[[DataBaseHandle shareDataBaseHandle]dropTable];//删除
    [self.mytableview setEditing:NO animated:YES];
    self.listData = [NSMutableArray array];
    
       }
//设置多少个区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;

}
//设置区中多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *array = [[DataBaseHandle shareDataBaseHandle]selectAllMovie];


    return  array.count;
}
//重用池
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellqwe = @"mycell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellqwe];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellqwe];
    }
    
    
    NSMutableArray *array = [[DataBaseHandle shareDataBaseHandle]selectAllMovie];
    Movie *movie = [[Movie alloc]init];
    movie = [array objectAtIndex:indexPath.row];
    NSLog(@"879789789%@",movie);
    cell.textLabel.text = movie.name;
    return cell;
}
//询问哪些行需要编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}

///设置编辑类型:删除或者插入
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleDelete;
}
//点击触发方法;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodDetailsViewController *fooddetails = [[FoodDetailsViewController alloc]init];
    ;
    [[DataBaseHandle shareDataBaseHandle]openDB];
    NSMutableArray *array = [[DataBaseHandle shareDataBaseHandle]selectAllMovie];
   fooddetails.movie =[array objectAtIndex:indexPath.row];
    NSLog(@"%@movie.namemovie.name",fooddetails.movie.name);
    NSLog(@"fooddetails.movie.imagePathLandscape ==%@",fooddetails.movie.imagePathLandscape);
    [self.navigationController pushViewController:fooddetails animated:YES];
}




-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (editingStyle==UITableViewCellEditingStyleDelete) {
            
            NSMutableArray *array = [[DataBaseHandle  shareDataBaseHandle]selectAllMovie];
            Movie *movie = [array objectAtIndex:indexPath.row];
            [[DataBaseHandle shareDataBaseHandle]deleteTable:movie.name];
            NSLog(@"array  ==%@",array);
            
            //[array removeObjectAtIndex:indexPath.row];
//            Movie *movie = [[Movie alloc]init];
//            movie = [array objectAtIndex:indexPath.row];
            
            [self.mytableview reloadData];
            NSLog(@"array.count ==%ld",array.count);
                   }
        
        
    }
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{

return @"删除";
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [self.mytableview reloadData];
    // NSLog(@"刷新页面 刷新页面刷新页面 ");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
